<template>
  <section class="main-content article-list">
    <el-col v-for="article in categoryDetailStore.categoryDetail" :key="article.id">
      <el-card shadow="hover" class="article-card" @click="getArticleDetail(article)">
        <div class="card-body">
          <img :src="article.cover_url || noPicture" alt="封面" class="cover-image"/>
          <div class="card-content">
            <h3 class="title">{{ article.title }}</h3>
            <p class="summary">{{ article.summary }}</p>
            <div class="meta">
              <!--              <span>分类：{{ article.category.name }}</span>-->
              <span>作者：{{ article.author }}</span>
              <span>时间：{{ dayjs(article.pub_time).format('YYYY-MM-DD HH:mm') }}</span>
            </div>
            <div class="tags">
              <el-tag v-for="tag in article.tags" :key="tag.id" type="info" class="tag" size="small">
                {{ tag.name }}
              </el-tag>
            </div>
          </div>
        </div>
      </el-card>
    </el-col>
  </section>
</template>
<script setup>
import {onMounted, watch} from 'vue'
import {useCategoryDetailStore} from '@/stores/category.js'
import noPicture from '@/assets/nopicture.jpg'
import dayjs from 'dayjs'
import {useRouter, useRoute} from 'vue-router'


const router = useRouter()
const route = useRoute()

const categoryDetailStore = useCategoryDetailStore()
const {id, slug} = route.params

onMounted(() => {
  categoryDetailStore.fetchCategoryDetail(id, slug)
})

// 路由参数变化时也要重新拉取数据
watch(() => [route.params.id, route.params.slug], ([newId, newSlug]) => {
  categoryDetailStore.fetchCategoryDetail(newId, newSlug)
})


const getArticleDetail = async (article) => {
  await router.push(`/articles/${article.id}/${article.slug}`)
}


</script>


<style scoped>


.article-card {
  margin-bottom: 5px;
  height: 100%;
}

.card-body {
  display: flex;
  gap: 20px;
}

.cover-image {
  width: 160px;
  height: 100px;
  object-fit: cover;
  border-radius: 8px;
  flex-shrink: 0;
}

.card-content {
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}

.title {
  font-size: 18px;
  font-weight: bold;
  margin-bottom: 8px;
}

.summary {
  color: #666;
  font-size: 14px;
  margin-bottom: 10px;
}

.meta {
  font-size: 12px;
  color: #999;
  margin-bottom: 10px;
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
}

.tags {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}
</style>